//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LmConstWrapperCorrelationModel.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Legacy/Libormarketmodels/LmCorrelationModel.h>
#include <gen/QL/Models/Parameter.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::CLmConstWrapperCorrelationModel (Cephei::QL::Legacy::Libormarketmodels::ILmCorrelationModel^ corrModel) : CLmCorrelationModel(CLmConstWrapperCorrelationModel::typeid)
{
    CLmCorrelationModel^ _CcorrModel;
    try
    {
#ifdef HANDLE
        _phLmConstWrapperCorrelationModel = NULL;
#endif
        _CcorrModel = safe_cast<CLmCorrelationModel^> (corrModel);
        _CcorrModel->Lock();
        boost::shared_ptr<QuantLib::LmCorrelationModel>& _corrModel = static_cast<boost::shared_ptr<QuantLib::LmCorrelationModel>&> (_CcorrModel->GetShared ()); 
        _ppLmConstWrapperCorrelationModel = new boost::shared_ptr<QuantLib::LmConstWrapperCorrelationModel> (new QuantLib::LmConstWrapperCorrelationModel ( _corrModel ));
        SetLmCorrelationModel (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmConstWrapperCorrelationModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcorrModel != nullptr) _CcorrModel->Unlock();
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::CLmConstWrapperCorrelationModel (boost::shared_ptr<QuantLib::LmConstWrapperCorrelationModel>& childNative, Object^ owner) : CLmCorrelationModel(CLmConstWrapperCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperCorrelationModel = NULL;
#endif
	_ppLmConstWrapperCorrelationModel = &childNative;
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmConstWrapperCorrelationModel));
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::CLmConstWrapperCorrelationModel (QuantLib::LmConstWrapperCorrelationModel& childNative, Object^ owner) : CLmCorrelationModel(CLmConstWrapperCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperCorrelationModel = NULL;
#endif
	_ppLmConstWrapperCorrelationModel = new boost::shared_ptr<QuantLib::LmConstWrapperCorrelationModel> (&childNative);
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmConstWrapperCorrelationModel));
    _LmConstWrapperCorrelationModelOwner = owner;
    _LmCorrelationModelOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::CLmConstWrapperCorrelationModel (CLmConstWrapperCorrelationModel^ copy) : CLmCorrelationModel(CLmConstWrapperCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperCorrelationModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLmConstWrapperCorrelationModel = new boost::shared_ptr<QuantLib::LmConstWrapperCorrelationModel> (copy->GetShared());
        _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmConstWrapperCorrelationModel));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::CLmConstWrapperCorrelationModel (PLATFORM::Type^ t) : CLmCorrelationModel(CLmConstWrapperCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperCorrelationModel = NULL;
#endif
	if (!t->IsSubclassOf(CLmConstWrapperCorrelationModel::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::CLmConstWrapperCorrelationModel (QuantLib::Handle<QuantLib::LmConstWrapperCorrelationModel>& childNative, Object^ owner)  : CLmCorrelationModel(CLmConstWrapperCorrelationModel::typeid)
{
	_phLmConstWrapperCorrelationModel = &childNative;
	_ppLmConstWrapperCorrelationModel = &static_cast<boost::shared_ptr<QuantLib::LmConstWrapperCorrelationModel>>(childNative.currentLink());
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmConstWrapperCorrelationModel));
    _LmConstWrapperCorrelationModelOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::CLmConstWrapperCorrelationModel (QuantLib::Handle<QuantLib::LmConstWrapperCorrelationModel> childNative)  : CLmCorrelationModel(CLmConstWrapperCorrelationModel::typeid)
{
	_phLmConstWrapperCorrelationModel = &childNative;
	_ppLmConstWrapperCorrelationModel = &static_cast<boost::shared_ptr<QuantLib::LmConstWrapperCorrelationModel>>(childNative.currentLink());
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmConstWrapperCorrelationModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::CLmConstWrapperCorrelationModel (QuantLib::LmConstWrapperCorrelationModel childNative)  : CLmCorrelationModel(CLmConstWrapperCorrelationModel::typeid)
{
#ifdef HANDLE
	_phLmConstWrapperCorrelationModel = NULL;
#endif
	_ppLmConstWrapperCorrelationModel = new boost::shared_ptr<QuantLib::LmConstWrapperCorrelationModel> (new QuantLib::LmConstWrapperCorrelationModel (childNative));
    _ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (boost::dynamic_pointer_cast<QuantLib::LmCorrelationModel> (*_ppLmConstWrapperCorrelationModel));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::~CLmConstWrapperCorrelationModel ()
{
    if (_ppLmConstWrapperCorrelationModel != NULL)
    {
	    delete _ppLmConstWrapperCorrelationModel;
        _ppLmConstWrapperCorrelationModel = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::!CLmConstWrapperCorrelationModel ()
{
    if (_ppLmConstWrapperCorrelationModel != NULL)
    {
	    delete _ppLmConstWrapperCorrelationModel;
    }
}
QuantLib::LmConstWrapperCorrelationModel& Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::GetReference ()
{
    if (_ppLmConstWrapperCorrelationModel == NULL) throw REFNEW NativeNullException ();
	return **_ppLmConstWrapperCorrelationModel;
}
boost::shared_ptr<QuantLib::LmConstWrapperCorrelationModel>& Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::GetShared ()
{
    if (_ppLmConstWrapperCorrelationModel == NULL) throw REFNEW NativeNullException ();
	return *_ppLmConstWrapperCorrelationModel;
}
QuantLib::LmConstWrapperCorrelationModel* Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::GetPointer ()
{
    if (_ppLmConstWrapperCorrelationModel == NULL) throw REFNEW NativeNullException ();
	return &**_ppLmConstWrapperCorrelationModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LmConstWrapperCorrelationModel>& Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::GetHandle ()
{
	if (_phLmConstWrapperCorrelationModel == NULL)
	{
		_phLmConstWrapperCorrelationModel = new Handle<QuantLib::LmConstWrapperCorrelationModel> (*_ppLmConstWrapperCorrelationModel);
	}
	return *_phLmConstWrapperCorrelationModel;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::HasNative () 
{
	return (_ppLmConstWrapperCorrelationModel != NULL);
}

Double Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::Correlation (UInt64 i, UInt64 j, Double t, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x))
        {
            _Cx = safe_cast<CArray^> (x->Value);
            _Cx->Lock();
        }
        QuantLib::Array& _x = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x) ? static_cast<QuantLib::Array&> (_Cx->GetReference ()) : QuantLib::Null<QuantLib::Array>()); //6
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLmConstWrapperCorrelationModel)->correlation ( _i,  _j,  _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
UInt64 Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::Factors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLmConstWrapperCorrelationModel)->factors ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel::IsTimeIndependent::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppLmConstWrapperCorrelationModel)->isTimeIndependent ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILmConstWrapperCorrelationModel^ Cephei::QL::Legacy::Libormarketmodels::CLmConstWrapperCorrelationModel_Factory::Create (Cephei::QL::Legacy::Libormarketmodels::ILmCorrelationModel^ corrModel)
{
    return REFNEW CLmConstWrapperCorrelationModel ( corrModel);
}
